package utils;

import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.util.Log;

public class GetJsonValue {
	String value;
	ArrayList<String> list = new ArrayList<String>();
	public static final String TAG = "yiqijiao";

	/**
	 * 传入JSON格式数据，和key，返回key对应的值。<br>
	 * 如果存在多个相同的key，本方法获取的是第一个key的值
	 * 
	 * @param jsonString
	 *            json格式数据
	 * @param key
	 *            想取键值对的键值
	 * @return value 返回键对应的值
	 * @throws JSONException
	 */
	public String getValue(String jsonString$, String key) throws JSONException {
		
		String jsonString=jsonString$.trim();
		
		JSONObject jsonObject = null;
		JSONArray jsonArray = null;
		if (jsonString.isEmpty()) {
			Log.e(TAG, "jsonString is null!");
			return null;
		}
		
		Object json = new JSONTokener(jsonString).nextValue();

		if (json instanceof JSONObject) {

			jsonObject = new JSONObject(jsonString);

			Iterator<?> it = jsonObject.keys();

			while (it.hasNext()) {

				String key$ = (String) it.next();

				if (key$.equals(key)) {

					value = jsonObject.optString(key);
					Log.w(TAG, "value++=" + value);

					return value;

				} else {
					if (jsonObject.optString(key$).isEmpty()) {
						Log.i(TAG, "the key:" + key$ + " value is null!!!");
					} else {
						getValue(jsonObject.optString(key$), key);
					}
				}
			}

		} else if (json instanceof JSONArray) {

			jsonArray = new JSONArray(jsonString);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jo = jsonArray.optJSONObject(i);

				Iterator<?> it = null;
				try {
					it = jo.keys();
				} catch (Exception e) {
					Log.e(TAG, "当前json数据没有key值");
					break;
				}
				while (it.hasNext()) {

					String key$ = (String) it.next();

					if (key$.equals(key)) {

						value = jo.optString(key);
						Log.w(TAG, "value**==" + value);
						return value;
					} else {
						if (jo.optString(key$).isEmpty()) {
							Log.i(TAG, "the key:" + key$ + " value is null!!!");
						} else {
							getValue(jo.optString(key$), key);
						}
					}
				}
			}
		}
		return value;
	}
	/**
	 * 获取当前JSONObject 对象中对应key的值(只在当前传入的json数据中查询)
	 * @param jsonString
	 * @param key
	 * @return
	 * @throws JSONException
	 */
	public String getCurrentValue(String jsonString$, String key) throws JSONException {
		
		String jsonString=jsonString$.trim();
		
		JSONObject jsonObject = null;
		JSONArray jsonArray = null;
		if (jsonString == null) {
			Log.e(TAG, "jsonString is null!");
			return null;
		}
		Object json = new JSONTokener(jsonString).nextValue();

		if (json instanceof JSONObject) {

			jsonObject = new JSONObject(jsonString);

			Iterator<?> it = jsonObject.keys();

			while (it.hasNext()) {

				String key$ = (String) it.next();

				if (key$.equals(key)) {

					value = jsonObject.optString(key);
					Log.w(TAG, "value++=" + value);

					return value;

				} else {
					if (jsonObject.optString(key$).isEmpty()) {
						Log.i(TAG, "the key:" + key$ + " value is null!!!");
					} 
				}
			}

		} else if (json instanceof JSONArray) {

			jsonArray = new JSONArray(jsonString);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jo = jsonArray.optJSONObject(i);

				Iterator<?> it = null;
				try {
					it = jo.keys();
				} catch (Exception e) {
					Log.e(TAG, "当前json数据没有key值");
					break;
				}
				while (it.hasNext()) {

					String key$ = (String) it.next();

					if (key$.equals(key)) {

						value = jo.optString(key);
						Log.w(TAG, "value**==" + value);
						return value;
					} else {
						if (jo.optString(key$).isEmpty()) {
							Log.i(TAG, "the key:" + key$ + " value is null!!!");
						} 
					}
				}
			}
		}
		return value;
	}

	/**
	 * 解析JSONObject 数据，返回所有的key所对应的值，存入list集合。
	 * 
	 * @param jsonString
	 * @param key
	 * @return 返回获取的所有的值的list集合
	 * @throws JSONException
	 */
	public ArrayList<String> getAllValue(String jsonString$, String key) throws JSONException {
		
		String jsonString=jsonString$.trim();
		
		JSONObject jsonObject = null;
		JSONArray jsonArray = null;

		if (jsonString == null) {
			Log.e(TAG, "jsonString is null!");
			return null;
		}
		Object json = new JSONTokener(jsonString).nextValue();

		if (json instanceof JSONObject) {

			jsonObject = new JSONObject(jsonString);

			Iterator<?> it = jsonObject.keys();
			while (it.hasNext()) {

				String key$ = (String) it.next();

				if (key$.equals(key)) {

					Log.w(TAG, "value++=" + jsonObject.optString(key));
					Log.e(TAG, "add=" + list.add(jsonObject.getString(key)));
					getAllValue(jsonObject.optString(key$), key);
				} else {
					if (jsonObject.optString(key$).isEmpty()) {
						Log.i(TAG, "JSONObject is null!!!");
					} else {
						getAllValue(jsonObject.optString(key$), key);
					}
				}
			}
		} else if (json instanceof JSONArray) {

			jsonArray = new JSONArray(jsonString);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jo = jsonArray.optJSONObject(i);

				Iterator<?> it = null;
				try {
					it = jo.keys();
				} catch (Exception e) {
					Log.e(TAG, "当前json数组可能不存在key值");
					break;
				}

				while (it.hasNext()) {

					String key$ = (String) it.next();

					if (key$.equals(key)) {
						Log.w(TAG, "value**==" + jo.optString(key));
						Log.e(TAG, "add=" + list.add(jo.getString(key)));
						getAllValue(jo.optString(key$), key);
					} else {
						if (jo.optString(key$).isEmpty()) {
							Log.i(TAG, "JSONObject is null!!!");
						} else {
							getAllValue(jo.optString(key$), key);
						}
					}
				}
			}
		}
		return list;
	}
}
